<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function array(arr) {
      let str = []
      for (let i = 1; i < arr.length; i++) {
        if (arr.indexOf(i) != arr.lastIndexOf(i)) {
          str.push(i)
          delete arr[i]

        }
        if (arr.indexOf(i) == -1) str.push(i)

      }

      // str.push(arr.length - str[0])

      return str
    }
    console.log(array([1, 2, 2, 4]))
  </script>
</body>

</html>